<?php

// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------


namespace app\index\controller;

use app\index\BaseController;
use app\index\model\charge\PileDayData;
use app\index\model\user\UserDayData;
use app\index\model\user\UserMonthData;
use app\index\model\user\UserPileRelation;
use Exception;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;
use think\facade\Request;


/**
 * Class Auth
 * @package app\controller\api
 * @author xaboy
 * @day 2020-05-06
 */
class UserProfit extends BaseController
{
    /**
     * 获取用户当天收益情况
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getUserDayProfit(){
        //我的充电桩
        $userId = $this->request->uid();
        [$page,$limit] = $this->getPage();
        $userPileModel = new UserPileRelation();
        $pileDataModel = new PileDayData();
        $getList = $userPileModel->getUserPileList($userId,$page,$limit);
        $getDay = date("Y-m-d");
        /*
        foreach ($getList['getList'] as $key=>$value){
            $pileData = $pileDataModel->getInfoByWhere(['pile_id'=>$value['relation_id'],'day_data'=>$getDay]);
            if($pileData){
                $getList['getList'][$key]['pile_data'] = $pileData;
            }else{
                $getList['getList'][$key]['pile_data'] = $this->getPileZeroData($getDay);
            }
        }
        */
        //获取当天的收益
        $userDayDataModel = new UserDayData();
        $userId = $this->request->uid();
        $dayWhere = ['user_id'=>$userId,'day_data'=>date("Y-m-d")];
        $getInfo = $userDayDataModel->getInfoByWhere($dayWhere);
        if(!$getInfo){
            $getInfo = [
                'day_data'=>date("Y-m-d"),
                'charge_electricity'=>0,
                'charge_money'=>0,
                'charge_number'=>0,
                'charge_time'=>0,
                'service_cost'=>0,
                'charge_cost'=>0,
            ];
        }
        return $this->success('获取成功',['pile_list'=>$getList,'day_profit'=>$getInfo]);
    }

    /**
     * 获取为0的数据
     * @return array
     */
    function getPileZeroData($getDay){
        return [
            'id'=>0,
            'day_data'=>$getDay,
            'charge_electricity'=>0,
            'charge_money'=>0,
            'charge_number'=>0,
            'charge_time'=>0,
            'service_cost'=>0,
            'charge_cost'=>0,
        ];
    }

    //获取月份奖励
    function userPileMonthProfit(){
        $getData = Request::post();
        if(!isset($getData['day_month']) || !$getData['day_month']) $getData['day_month'] = date("Y-m",strtotime("-1 month"));
        $userId = $this->request->uid();
        $monthWhere = ['user_id'=>$userId,'day_month'=>$getData['day_month']];
        $userMonthDataModel = new UserMonthData();
        $lastMonthData = $userMonthDataModel->getInfoByWhere($monthWhere);
        if(!$lastMonthData){
            $lastMonthData =  [
                'user_id'=>$userId,
                'day_month'=>$getData['day_month'],
                'charge_electricity'=>0,
                'charge_money'=>0,
                'charge_number'=>0,
                'charge_time'=>0,
                'service_cost'=>0,
                'charge_cost'=>0,
                'carbon_dioxide_number'=>0,
                'tree_planting_number'=>0,
                "real_charge_electricity"=>0,
                "real_charge_money"=>0,
                "real_charge_number"=>0,
                "real_charge_time"=>0,
                "real_service_cost"=> 0,
                "real_charge_cost"=>0
            ];
        }
        return $this->success('获取成功',$lastMonthData);
    }


    /**
     * 我的充电桩获利
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function userPileProfit(){
        $userDayDataModel = new UserDayData();
        $userId = $this->request->uid();
        $dayWhere = ['user_id'=>$userId];
        [$page,$limit] = $this->getPage();
        $dayDataList = $userDayDataModel->getUserDayList($dayWhere,$page,$limit);
        return $this->success('获取成功',$dayDataList);
    }



}
